'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskTaskdetailCtrl
 * @description
 * # TaskTaskdetailCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('TaskTaskdetailCtrl', function($scope, openApi, DataService, $location, Util, $rootScope, $filter, $routeParams, TaskStaticData, $q) {
        var vm = this;
        this.param = {};
        this.saveInfo = function() {
            var params = this.param;
            // $routeParams.id通过id确定唯一回显id
            Util.savaParamForReview(params, "TaskNewtaskCtrl");
        };
        this.goaddjoinpeople = function(type) {
            if (this.param.isManager != 3) {
                return
            }
            this.saveInfo();
            $location.path("task/addjoinpeople/" + type)
        };
        this.showSheet = function(type) {
            if (!(this.param.isManager == 3 || this.param.isManager == 2 && type == "statusObj")) {
                return
            }
            var sheetObj = this.sheetObj[type]
            this.sheetObj.type = type
            this.sheetObj.tabList = sheetObj.tabList
            this.sheetObj.sheettitle = sheetObj.sheettitle
            this.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true

        };
        //------------------------------多选框
        // 底部各种类型选框添加
        this.sheetObj = {};
        // ---------------------------------
        var statusObj = {};
        statusObj.sheettitle = "创建任务至";
        statusObj.tabList = TaskStaticData.statusList;
        statusObj.callback = function(item) {
            vm.param.status = item.selectTab
            vm.param.statusContent = item.selectTabContent
        };
        // ---------------------------------
        var priorityObj = {};
        priorityObj.sheettitle = "优先级";
        priorityObj.tabList = TaskStaticData.priorityList;
        priorityObj.callback = function(item) {
            vm.param.priority = item.selectTab
            vm.param.priorityContent = item.selectTabContent
        };
        // -----------------------------------
        var repetitionObj = {};
        repetitionObj.sheettitle = "";
        repetitionObj.tabList = TaskStaticData.repetitionList;
        repetitionObj.callback = function(item) {
            vm.param.repetition = item.selectTab
            vm.param.repetitionContent = item.selectTabContent
        };
        // ---------------------------------
        var startNoticeTimeObj = {};
        startNoticeTimeObj.sheettitle = "任务开始提醒";
        startNoticeTimeObj.tabList = TaskStaticData.remindList;
        startNoticeTimeObj.callback = function(item) {
            vm.param.startNoticeTime = item.selectTab
            vm.param.startNoticeTimeContent = item.selectTabContent
        };
        var endNoticeTimeObj = {};
        endNoticeTimeObj.sheettitle = "任务结束提醒";
        endNoticeTimeObj.tabList = TaskStaticData.remindList;
        endNoticeTimeObj.callback = function(item) {
            vm.param.endNoticeTime = item.selectTab
            vm.param.endNoticeTimeContent = item.selectTabContent
        };
        // -----------------------------------
        this.sheetObj.statusObj = statusObj;
        this.sheetObj.priorityObj = priorityObj;
        this.sheetObj.repetitionObj = repetitionObj;
        this.sheetObj.startNoticeTimeObj = startNoticeTimeObj;
        this.sheetObj.endNoticeTimeObj = endNoticeTimeObj;
        this.submit = function() {
            var param;
            param = angular.copy(this.param);
            console.log(param);
            if (param.subTasks && param.subTasks.length > 0) {
                this.param.subTaskStr = JSON.stringify(param.subTasks)
            }
            if (param.joinPersons) {
                var userIds = param.joinPersons.map(function(item) {
                    return {
                        userId: item.userId
                    }
                })
                param.joinPersionStr = JSON.stringify(userIds);
            }
            delete param.subTasks;
            delete param.joinPersons;
            var ckeched = this.checksubmit();
            if (!ckeched) {
                return
            }
            DataService.getData(openApi.updateTask, param).then(function(data, success) {
                Util.Toast(data, {
                    callback: function() {
                        Util.goBack();
                    }
                })
            }.bind(this))

        };
        this.checksubmit = function() {
            var globalcheck = true;
            if (!this.param.title) {
                Util.Toast("请填写任务名称")
                globalcheck = false;
                return globalcheck
            }
            if (!this.param.endDate) {
                Util.Toast("请选择结束日期")
                globalcheck = false;
                return globalcheck
            }
            return globalcheck

        };
        vm.deljoinconfirm = function(index) {
            vm.curdeljoinindex = index;
            if (navigator.notification) {
                navigator.notification.confirm(
                    '确定删除吗？',
                    vm.deljoinper,
                    '提示', ['确定', '取消']
                );
            } else {
                vm.deljoinper(1)
            }
        };
        vm.deltaskconfirm = function(index) {
            vm.curdeltaskindex = index;
            if (navigator.notification) {
                navigator.notification.confirm(
                    '确定删除吗？',
                    vm.deltask,
                    '提示', ['确定', '取消']
                );
            } else {
                vm.deltask(1)
            }
        };
        vm.deltask = function(button) {
            var taskItem, param = {};
            taskItem = vm.param.subTasks[vm.curdeltaskindex];
            param.pkId = taskItem.pkId
            if (button == 1) {
                DataService.getData(openApi.delSubTask, param).then(function(data) {
                    Util.Toast(data)
                    vm.param.subTasks.splice(vm.curdeltaskindex, 1);
                }.bind(vm))
            }
        };
        this.changetaskStatus = function(item) {
            var param = {},
                url;
            param.subTaskId = item.pkId
            if (item.finished) {
                url = openApi.startSubTask;
            } else {
                url = openApi.finishSubTask;
            }
            DataService.getData(url, param).then(function(data) {
                Util.Toast(data);
                item.finished = !item.finished
            }.bind(this))

        };
        vm.deljoinper = function(button) {
            var checked;
            if (button == 1) {
                checked = vm.checkifinsubtask();
                if (checked) {
                    Util.Toast("该参与人有负责的子任务")
                    return;
                }
                var param = {},
                    userItem, userId;
                userItem = vm.param.joinPersons[vm.curdeljoinindex];
                userId = userItem.userId
                param.refId = vm.param.pkId
                param.userId = userId
                DataService.getData(openApi.delJoinPerson, param).then(function(data) {
                    Util.Toast(data)
                    vm.param.joinPersons.splice(vm.curdeljoinindex, 1);
                    $scope.$apply();
                }.bind(vm))
            }
        };
        vm.checkifinsubtask = function() {
            var checked = false;
            var user = {};
            user = vm.param.joinPersons[vm.curdeljoinindex];
            if (vm.param.subTasks && vm.param.subTasks.length > 0) {
                vm.param.subTasks.forEach(function(item, index) {
                    if (item.userId == user.userId) {
                        checked = true;
                        return;
                    }
                });
            }
            return checked;
        };
        this.addsubtask = function() {
            this.saveInfo();
            $location.path("task/subtask/taskDetail")
        };
        this.togglePublic = function() {
            if (this.param.isManager != 3) {
                return
            }
            this.param.visibilityInPublic = this.param.visibilityInPublic ? 0 : 1;
            this.param.visibilityInPublicContent = TaskStaticData.getContentBykey(this.param.visibilityInPublic, "visibilityInPublic");;
        };
        this.getUserinfo = function() {
            DataService.getData(openApi.userInfo).then(function(data) {
                // 如果没有用户id默认为当前用户
                if (!this.param.userId) {
                    this.param.userId = data.userId
                    this.param.userName = data.name
                }
            }.bind(this))
        };
        // 根据返回的key来设置显示内容
        this.setContentForKey = function() {
            // 设置任务状态
            // this.param.status = this.param.status || TaskStaticData.statusList[0].value;
            this.param.statusContent = TaskStaticData.getContentBykey(this.param.status, "status");
            // 设置开始时间
            // this.param.startDate = this.param.startDate || $filter("dateFmt")(new Date())
            // 设置优先级
            // this.param.priority = this.param.priority || TaskStaticData.priorityList[0].value;
            this.param.priorityContent = TaskStaticData.getContentBykey(this.param.priority, "priority");
            // 设置重复
            // this.param.repetition = this.param.repetition || TaskStaticData.repetitionList[0].value;
            this.param.repetitionContent = TaskStaticData.getContentBykey(this.param.repetition, "repetition");
            // 设置开始提醒
            // this.param.startNoticeTime = this.param.startNoticeTime || TaskStaticData.remindList[1].value;
            this.param.startNoticeTimeContent = TaskStaticData.getContentBykey(this.param.startNoticeTime, "remind");
            // 设置完成提醒
            // this.param.endNoticeTime = this.param.endNoticeTime || TaskStaticData.remindList[1].value;
            this.param.endNoticeTimeContent = TaskStaticData.getContentBykey(this.param.endNoticeTime, "remind");
            // 设置权限
            // this.param.visibilityInPublic = this.param.visibilityInPublic || TaskStaticData.visibilityInPublicList[0].value;
            this.param.visibilityInPublicContent = TaskStaticData.getContentBykey(this.param.visibilityInPublic, "visibilityInPublic");
        };
        this.getTaskDetail = function() {
            var param = {};
            param.pkId = $routeParams.pkId
            DataService.getData(openApi.getTaskDetail, param).then(function(data) {
                // 如果没有用户id默认为当前用户
                this.param = data;
                // this.param.startDate = $filter("dateFmt")(this.param.startDate);
                // this.param.endDate = $filter("dateFmt")(this.param.endDate);
                // 根据返回的key来设置显示内容
                this.setContentForKey()
            }.bind(this))
        };
        this.getcommet = function() {
            this.showcommetlist = true
            this.showloglist = false
            var param = {};
            param.apitype = "task"
            param.refId = $routeParams.pkId
            DataService.getData(openApi.getworktaskReviewlist, param).then(function(data) {
                this.commetList = data
            }.bind(this))

        }
        this.commethandle = function(param) {
            var delay = $q.defer();
            param.refId = $routeParams.pkId
            param.apitype = "task";
            DataService.getData(openApi.addForWorktask, param).then(function() {
                vm.getcommet()
                delay.resolve();
            }.bind(this));
            return delay.promise;
        }
        this.showfile = function(file) {
            if (file.match((/.((jpg)|(png)|(gif)|(jpeg))+/g))) {
                $rootScope.showMasker = true;
                this.src = openApi.showimg + "?fileName=" + file
            } else {
                Util.Toast("移动端不支持预览")
            }

        };
        this.showcommet = function() {
            this.getcommet()
        };
        this.showlog = function() {
            this.showcommetlist = false
            this.showloglist = true
            var param = {};
            param.apitype = "task"
            param.refId = $routeParams.pkId
            DataService.getData(openApi.getLoglist, param).then(function(data) {
                this.loglist = data
            }.bind(this))
        };
        this.closefile = function() {
            $rootScope.showMasker = false;
            this.src = "";
        };
        this.init = function() {
            this.param = Util.getParamForReview("TaskNewtaskCtrl")
                // this.getUserinfo();
                // this.setDeault();
            if (!this.param.form_id) {
                this.getTaskDetail()
            }
            this.getcommet()
        };
        this.init();
    });
